<?php
	$tgl = date("d");
	$bln = date("m");
	$thn = date("Y");
	
	$periode = $_POST['periode'];
	$tgl1 = $_POST['tgl1'];
	$tgl2 = $_POST['tgl2'];
	$bln1 = $_POST['bln1'];
	$bln2 = $_POST['bln2'];
	$thn1 = $_POST['thn1'];
	$thn2 = $_POST['thn2'];
	$jns = $_POST['jenis'];
	$wlyh = $_POST['wilayah'];
	
	if($periode == 1){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-$tgl1'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-$tgl2'";
	}else if($periode == 2){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-31'";
	}else{
		$tgl1 = "and p.tgl_mulai >= '$thn1-01-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-12-31'";
	}
	
	if($jns == ''){
		$hsl_jns = "";
	}else{
		$hsl_jns = "and p.id_jenis_peserta = $jns";
	}
	
	if($wlyh == ''){
		$sql="select kc.id_kecamatan, kc.kecamatan,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa where d.id_kecamatan = kc.id_kecamatan $hsl_jns $tgl1 $tgl2)  as jml from kecamatan kc where id_kabupaten = '6405'";
	}else{
		$sql="select ds.id_desa, ds.desa,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa where d.id_desa = ds.id_desa $hsl_jns $tgl1 $tgl2)  as jml from desa ds where ds.id_kecamatan = '$wlyh'";
	}
		
	if($_POST['submit'] == 'Proses'){
	?>
		<table class="tabel">
			<tr>
				<th>NO</th>
				<th>Nama Wilayah</th>
				<th>Jumlah</th>
			</tr>
			<?php
				//echo $sql;
				$qry=mysql_query($sql);
					while($hasil=mysql_fetch_array($qry)){
						$no++;
			?>
						<tr>
							<td align="center" width="30"><?php echo $no;?></td>
							<td >
								<?php 
									if($wlyh == ''){
										echo $hasil['kecamatan'];
									}else{
										echo $hasil['desa'];
									}
								?>
							</td>
							<td align="center" align="center" width="60"><?php echo $hasil['jml'];?></td>
						</tr>
						
				<?php
						$jumlah = $jumlah + $hasil['jml'];
					}
				?>
			<tr>
				<td align="center" colspan="2"><b>Jumlah</b></td>
				<td align="center" align="center" width="60"><?php echo "<b>$jumlah</b>";?></td>
			</tr>
		</table>
	<?php
	}
	?>